# -*- coding: UTF-8 -*-
'''
Created on 2010-12-27

@author: sumory
'''

import os
import sys
import datetime
from PyQt4.QtGui import *
from PyQt4.QtCore import *

from ui_qaoba import Ui_mainWindow
from utils import iencrypt,my_utils
from config import config as myconfig

class MainWindow(QMainWindow, Ui_mainWindow):
    
    """
    QaoBa's main window
    """
    def __init__(self, parent = None):
        """
        Constructor
        """
        super(MainWindow, self).__init__(parent)

        self.setupUi(self)
        self.init_control_data()
        self.originalPalette = QApplication.palette()
        self.changeStyle('Plastique')
        
    def init_control_data(self):
        pass

    def update_settings(self):
        pass
    
    def changeStackedWidget(self):
        self.stack.setCurrentIndex(0)
        
    def changeStackedWidget1(self):
        self.stack.setCurrentIndex(1)
        
    def openEncryptDir_performed(self):
        options = QFileDialog.DontResolveSymlinks | QFileDialog.ShowDirsOnly
        directory = QFileDialog.getExistingDirectory(self,
                u"请选择要加密的文件夹",
                self.pathLineEdit.text(), options)
        if directory:
            self.pathLineEdit.setText(directory)
            
    def openDecryptDir_performed(self):
        options = QFileDialog.DontResolveSymlinks
        directory = QFileDialog.getOpenFileName(self,
                u"请选择要解密的日志文件",
                self.pathLineEdit.text(),'',None, options)
        if directory:
            self.pathLineEdit.setText(directory)
                  
    def encryptButton_performed(self):
        dir=str(self.pathLineEdit.text()).decode('utf-8')
        if(dir==''):
            QMessageBox.warning(self, "Warning", QString.fromUtf8('请确定已找到正确目录'), QMessageBox.Ok)
        else:
            do_ok=QMessageBox.question(self,"Question",
                                    QString.fromUtf8('即将加密的目录是：\n'+dir+'\n您确定要执行加密操作吗？'),
                                    QMessageBox.Ok|QMessageBox.Cancel,
                                    QMessageBox.Ok)
            if do_ok==QMessageBox.Ok:
                dir='/'.join(dir.split('\\'))
                outfile=myconfig.get(my_utils.getCurTime(2)+'.txt')#"D:/binaries.dat"
                #suffix = ".c|.cpp"
                suffix = ".*"
                logs=iencrypt.encrypt(dir,outfile,suffix,1)
                self.textEdit.setText(logs)
            else:
                pass
            
    def decryptButton_performed(self):
        dir=str(self.pathLineEdit.text()).decode('utf-8')
        if(dir==''):
            QMessageBox.warning(self, "Warning", QString.fromUtf8('请确定已找到正确的解密日志文件'), QMessageBox.Ok)
        else:
            dir='/'.join(dir.split('\\'))
            feedback=iencrypt.decrypt(dir)
            if feedback=='Exception':
                feedback='解密过程发生异常，请检查是否加密后文件有过变动或您执行了不合法的错误操作'.decode('utf-8')
                QMessageBox.critical(self, "Error", QString.fromUtf8('解密过程发生异常,请检查目录或文件是否仍存在!'))
            self.textEdit.setText(feedback)
            
    def action_about_performed(self):
        QMessageBox.about(self, "About QaoBa",
                "The <b>QaoBa</b> application is a toolkit for some useful functions.")
    
    def action_contact_performed(self):
        QMessageBox.about(self, "About author",
                "The <b>QaoBa</b> application is created by <b>Sumory</b>. <br/>You can contact with me using this email:<br/>sumory.wu@gmail.com")
   
    def closeEvent(self):
        self.update_settings()
        #self.mask()
        if QMessageBox.question(self,
            QString.fromUtf8('友情提示'),
            QString.fromUtf8('您确定要退出QaoBa?'),
            QMessageBox.Yes|QMessageBox.No) == QMessageBox.Yes:
            super(MainWindow,self).close()
       
    def changeStyle(self, styleName):
        QApplication.setStyle(QStyleFactory.create(styleName))
        self.changePalette()

    def changePalette(self):
        if (self.useStylePaletteCheckBox.isChecked()):
            QApplication.setPalette(QApplication.style().standardPalette())
        else:
            QApplication.setPalette(self.originalPalette)
            
if __name__ == "__main__":
    app = QApplication(sys.argv)
    mainForm = MainWindow()
    mainForm.show();
    sys.exit(app.exec_())
